
import pyttsx3
from threading import Thread
from playsound import playsound

"""
    text: 读入文本
    id: 为对象起名
    lang: 读入文本语言类型
    def exec: 生成语音文件
    def play: 生成语音文件并播放
"""
class txtMp3():
    def __init__(self, text='测试文本', id='voice', lang='zh', mp3Path='text.wav'):
        self.text = text
        self.id = id
        self.lang = lang
        self.mp3Path = mp3Path

    def exec(self):
        engine = pyttsx3.init()
        engine.setProperty(self.id, self.lang)
        engine.save_to_file(self.text, self.mp3Path)
        engine.runAndWait()

    def play(self):
        self.exec()
        Thread(target=playsound, args=(self.mp3Path,)).start()

